<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

class ContactController extends AppController {
	public $uses = array('Contact', 'Category');
	public $helpers = array ('Html', 'Form', 'Captcha');
	public $components = array(
		'Session',
		'Common',
		'Captcha',
	);

	public function index(){
		if ($this->request->is('post')) {
			$params = $this->request->data;
			$this->Contact->set($params);
			$params['Contact']['create_time'] = time();

			if ($this->Contact->valid() == true) {
				if ($this->Contact->save($params)) {

					$this->redirect('/');
				}
			} else {
				$this->Session->setFlash("Data is not avaliable !");
			}
		}
	}

	public function view($type){
		if ($type == 'lien-he') {
			if ($this->request->is('post')) {
				$params = $this->request->data;
				$this->Contact->create();
				$params['Contact']['create_time'] = time();
				$params['Contact']['last_update'] = time();
				$params['Contact']['last_update'] = time();
				$params['Contact']['type'] = 1;

				if ($this->Contact->valid() == true) {
					if($this->Captcha->validateCaptcha()) {
						if ($this->Contact->save($params)) {
							//send mail
							try {
								$message = $params['Contact']['message'];
								$Email = new CakeEmail($this->gmail);
								$Email->from(array($this->company['Configuration']['gmail_name'] => $this->company['Configuration']['name']))
								->emailFormat('html')
								->to($this->company['Configuration']['email'])
								->subject('Đăng ký online')
								->send($message);
							} catch (Exception $ex) {
								CakeLog::error('contact online send mail error' . $ex->getMessage());
								continue;
							}
							$this->redirect('/');
						}
					}
				} else {
					$this->Session->setFlash("Data is not avaliable !");
				}
			}

			$url = '/lien-he/van-phong-cong-chung-au-lac.html';
			BreadcrumbComponent::set('Liên hệ', $url);

			$title_for_layout = "Liên hệ ". $this->company['Configuration']['name'];
			$keywords_for_layout = $this->company['Configuration']['keywords'];
			$description_for_layout = $this->company['Configuration']['description'];


			$this->set(
					array(
							'title_for_layout' => $title_for_layout,
							'keywords_for_layout' => $keywords_for_layout,
							'description_for_layout' => $description_for_layout,
							'breadcrumb' => BreadcrumbComponent::create_links(),
					)
			);

			$this->render('contact');
		} else {
			if ($this->request->is('post')) {
				$params = $this->request->data;
				$this->Contact->create();
				$params['Contact']['create_time'] = time();
				$params['Contact']['last_update'] = time();
				$params['Contact']['type'] = 2;

				if ($this->Contact->valid() == true) {
					if($this->Captcha->validateCaptcha()) {
						if ($this->Contact->save($params)) {
							//send mail
							try {
								$message = $params['Contact']['message'];
								$Email = new CakeEmail($this->gmail);
								$Email->from(array($this->company['Configuration']['gmail_name'] => $this->company['Configuration']['name']))
								->emailFormat('html')
								->to($this->company['Configuration']['email'])
								->subject('Đăng ký online')
								->send($message);
							} catch (Exception $ex) {
								CakeLog::error('register online send mail error' . $ex->getMessage());
								continue;
							}

							$this->redirect('/');
						}
					}

				} else {
					$this->Session->setFlash("Data is not avaliable !");
				}
			}

			$url = '/dang-ky-online/van-phong-cong-chung-au-lac.html';
			BreadcrumbComponent::set('Đăng ký online', $url);

			$title_for_layout = "Đăng ký hồ sơ online ". $this->company['Configuration']['name'];
			$keywords_for_layout = "Đăng ký hồ sơ online ". $this->company['Configuration']['keywords'];
			$description_for_layout = "Đăng ký hồ sơ online ". $this->company['Configuration']['description'];

			$this->set(
					array(
							'title_for_layout' => $title_for_layout,
							'keywords_for_layout' => $keywords_for_layout,
							'description_for_layout' => $description_for_layout,
							'breadcrumb' => BreadcrumbComponent::create_links(),
					)
			);

			$this->render('register');
		}

	}

	public function captcha()
	{
		$this->Captcha->configCaptcha(
				array(
						'width '=> 200,//image width -default 200
						'height' => 70,//image height-default 70
						'quality' => 2,//set quality-high:3 ,mid:2 ,low:1
						'blur' => false,//enablle/disable blur
						'format' => 'jpeg',//iage formats. jpeg or png
						'wordFile' => false,//include word file or not
						'minLength' => 5,//max word length
						'maxLength' => 7,//min. word length
						//'background' => array(87,87,87), //rgb color code here
						'color' => array(
								array(128,0,255), //each color code in rgb in form of array
								array(0,255,255),
								array(255,0,0),
								//    true //uncomment if overwrite existing colors,comment if append new colors to existing ones
						),
						'pathType' => 2 //default:1.The Alternate path type    1 or 2.change pathtype to 2 if 1 does not work.
				)
		);
		$this->Captcha->getCaptcha();
	}

}